<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

		  <p-col  :span="12">
			<p-form-item label="工站编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-popup
						contextPath="basic-business"
						v-decorator="['workstationcode', validatorRules.workstationcode]"
						:trigger-change="true"
						org-fields="wpno,wpname"
						dest-fields="workstationcode,workstationname"
						code="mes_q_ba_station"
						@callback="popupCallback"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="工站名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'workstationname', validatorRules.workstationname]" placeholder="请输入工站名称"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'fillingqty', validatorRules.fillingqty]" placeholder="请输入数量" style="width: 100%"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="工单数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'qty', validatorRules.qty]" placeholder="请输入工单数量"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-popup
						contextPath="basic-business"
						v-decorator="['mcode', validatorRules.mcode]"
						:trigger-change="true"
						org-fields="id,mcode,mname,sprc,mexplain"
						dest-fields="mid,mcode,mname,sprc,mexplain"
						code="wms_cpinfo"
						@callback="popupCallback"/>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'mname', validatorRules.mname]" placeholder="请输入物料名称"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="物料规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'sprc', validatorRules.sprc]" placeholder="请输入物料规格型号"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="物料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'mexplain', validatorRules.mexplain]" placeholder="请输入物料描述"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="snpn" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'snpn', validatorRules.snpn]" placeholder="请输入snpn"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="附件编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'attachmentCoding', validatorRules.attachmentCoding]" placeholder="请输入附件编码"></p-input>
			</p-form-item>
		  </p-col>

		  <p-col  :span="12">
			<p-form-item label="绑定状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['bindingState', validatorRules.bindingState]" :trigger-change="true" dictCode="mes_binding_state" placeholder="请选择绑定状态"/>
			</p-form-item>
		  </p-col>


        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="包装采集(人工包装)历史记录" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="mesMfHandFillingHistoryTable.loading"
            :columns="mesMfHandFillingHistoryTable.columns"
            :dataSource="mesMfHandFillingHistoryTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'MesMfHandFillingModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          workstationcode: {rules: [
            {required: true, message: '请输入工站编码!'},
          ]},
          workstationname: {rules: [
          ]},
          fillingqty: {rules: [
            {required: true, message: '请输入数量!'},
          ]},
          qty: {rules: [
          ]},
          mcode: {rules: [
            {required: true, message: '请输入物料编码!'},
          ]},
          mname: {rules: [
            {required: true, message: '请输入物料名称!'},
          ]},
          sprc: {rules: [
          ]},
          mexplain: {rules: [
          ]},
          snpn: {rules: [
          ]},
          attachmentCoding: {rules: [
            {required: true, message: '请输入附件编码!'},
          ]},
          bindingState: {rules: [
            {required: true, message: '请输入绑定状态!'},
          ]},
        },
        refKeys: ['mesMfHandFillingHistory', ],
        tableKeys:['mesMfHandFillingHistory', ],
        activeKey: 'mesMfHandFillingHistory',
        // 包装采集(人工包装)历史记录
        mesMfHandFillingHistoryTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '替换人员标识',
              key: 'substituteId',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '生产数量',
              key: 'fillingqty',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: 'snpn',
              key: 'snpn',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '工序工单编号',
              key: 'mobillno',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '工单数量',
              key: 'qty',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '已包装数量',
              key: 'packedQuantity',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '待包装数量',
              key: 'quantityPacked',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '包装人员',
              key: 'packagingWorker',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '入库数量',
              key: 'inventoryQty',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '入库时间',
              key: 'warehouseEntrytime',
              type: FormTypes.date,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '送检状态',
              key: 'inspectionStatus',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料编码',
              key: 'mcode',
              type: FormTypes.popup,
              contextPath: 'basic-business',
              popupCode: 'wms_cpinfo',
              destFields: 'mid,mcode,mname,sprc,mexplain',
              orgFieldse: 'id,mcode,mname,sprc,mexplain',
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '物料名称',
              key: 'mname',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '物料规格型号',
              key: 'sprc',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料描述',
              key: 'mexplain',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },

            {
              title: 'mid',
              key: 'mid',
              type: 'hidden'
            },




          ]
        },
        url: {
          add: '/mes-manufacture/auto/mesMfHandFilling/add',
          edit: '/mes-manufacture/auto/mesMfHandFilling/edit',
          mesMfHandFillingHistory: {
            list: '/mes-manufacture/auto/mesMfHandFilling/queryMesMfHandFillingHistoryByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'workstationcode','workstationname','fillingqty','qty','mcode','mname','sprc','mexplain','snpn','attachmentCoding','bindingState','createBy','createTime','updateBy','updateTime')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.mesMfHandFillingHistory.list, params, this.mesMfHandFillingHistoryTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          mesMfHandFillingHistoryList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'workstationcode','workstationname','fillingqty','qty','mcode','mname','sprc','mexplain','snpn','attachmentCoding','bindingState','createBy','createTime','updateBy','updateTime'))
     },

    }
  }
</script>

<style scoped>
</style>
